﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShiXuLong.Service;
using ShiXuLong.Model;
using ShiXuLong.Service.New;

namespace ShiXuLong.Admin.Controllers
{

    public class HomeController : Controller
    {

        Login cd = new Login();
        ShopManagement sp = new ShopManagement();
        // GET: Home
        Service.New.User user = new Service.New.User();
        /// <summary>
        /// 后台主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var list = user.Getuser();//详情见方法，

            ViewBag.UserId = list.Id;
            ViewBag.UserImage = list.UserImage;
            Login();
            return View();
        }

        /// <summary>
        /// 做一个虚拟登录的功能，在点击个人资料的时候，调用这个方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            //var list= user.Getuser();
            //ViewBag.UserId = list.Id;
            return Json(new { success = true });
        }

        /// <summary>
        /// 商品管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ShopView()
        {
            return View();
        }
        public ActionResult Welcome()
        {
            return View();
        }
        /// <summary>
        /// 商品数据接口
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="ShopName"></param>
        /// <param name="ShopType"></param>
        /// <returns></returns>
        public ActionResult Shop(int limit, int page, string shopName, string shopTypeName)
        {
            int count = 0;
            var list = sp.GetShop(limit, page, shopName, shopTypeName, out count);
            //list = list.Take(limit).Skip(limit * (page-1)).ToList();
            list = list.Skip(limit * (page - 1)).Take(limit).ToList();
            return Json(new { code = 0, msg = "", count = count, data = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 上下架功能页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShopStatusView(int id)
        {
            var list = sp.GetShopById(id);
            return View(list[0]);
        }

        /// <summary>
        /// 上下架接口
        /// </summary>
        /// <param name="status"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShopStatus(int id)
        {
            var list = sp.GetShopById(id);

            int res = sp.UpShopStatus(list.SingleOrDefault());
            if (res > 0)
            {
                return Json(new { success = "true", msg = "成功" });
            }
            else
            {
                return Json(new { success = "false", msg = "失败" });
            }
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddView()
        {
            ViewBag.ShopType = sp.GetShopType();
            return View();
        }

        /// <summary>
        /// 添加接口
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public ActionResult Add(Shop shop)
        {
            int res = sp.AddShop(shop);
            if (res > 0)
            {
                return Json(new { success = res, msg = "成功" });
            }
            else
            {
                return Json(new { success = res, msg = "失败" });
            }

        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditView(int id)
        {
            ViewBag.ShopType = sp.GetShopType();
            var list = sp.GetShopById(id);
            return View(list[0]);
        }

        /// <summary>
        /// 编辑接口
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public ActionResult Edit(Shop shop)
        {
            int res = sp.EditShop(shop);
            if (res > 0)
            {
                return Content("成功");
            }
            return Content("失败");
        }

        /// <summary>
        /// 删除页面
        /// </summary>
        /// <returns></returns>
        public ActionResult DelView()
        {
            return View();
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(int id)
        {
            int res = sp.DelShop(id);
            if (res > 0)
            {
                return Content("成功");
            }
            return Content("失败");

        }


        /// <summary>
        /// 个人资料页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PersonalData(int id = 31)
        {
            var list = user.GetUser1(id);
            ViewBag.User = list;
            //ViewBag.PersonalICon = list;
            return View();
        }

        /// <summary>
        /// 图片上传接口
        /// </summary>
        /// <returns></returns>
        public ActionResult UserImageUpdate()
        {
            try
            {
                //上传的单独文件的访问
                HttpPostedFileBase postFile = Request.Files["file"];
                if (postFile != null)
                {
                    //文件扩展名，不含“.”
                    string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1);
                    //随机生成新的文件名
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt;
                    //上传目录相对路径~表示网站根目录
                    string upLoadPath = "~/Content/image/";
                    //上传目录的物理路径
                    string fullUpLoadPath = Server.MapPath(upLoadPath);
                    //核心方法，上传文件并保存
                    postFile.SaveAs(fullUpLoadPath + newFileName);
                    return Json(new { success = true, message = "", remark = "Content/image/" + newFileName });
                }
                else
                {
                    return Json(new { success = false, message = "未检测到文件数据" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "上传失败:" + ex.Message });
            }
        }

        /// <summary>
        /// 更新个人资料的接口
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>

        public ActionResult UpdateUser(ShiXuLong.Model.User data)
        {
            var re = user.UpdateUser(data);
            if (re > 0)
            {
                return Json(new { success = true, msg = "保存成功" });
            }
            else
            {
                return Json(new { success = false, msg = "保存失败" });
            }


        }
        /// <summary>
        /// 修改密码页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateUserPwdView()
        {
            return View();
        }
        public ActionResult UpdateUserPwd(string NewPwd, string OldPwd)
        {
            var user1 = user.Getuser();

            if (user1.UserPwd.Equals(OldPwd))
            {
                var re = user.UpdateUserPwd(NewPwd, user1);
                if (re > 0)
                {
                    return Json(new { success = true, msg = "修改密码成功" });
                }
                else
                {
                    return Json(new { success = false, msg = "修改密码失败，原因未知" });
                }
            }
            else
            {
                return Json(new { success = false, msg = "旧密码输入错误" });
            }


        }

        public ActionResult UpdateShopImage()
        {

            try
            {
                //上传的单独文件的访问
                HttpPostedFileBase postFile = Request.Files["file"];
                if (postFile != null)
                {
                    //文件扩展名，不含“.”
                    string fileExt = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".") + 1);
                    //随机生成新的文件名
                    string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "." + fileExt;
                    //上传目录相对路径~表示网站根目录
                    string upLoadPath = "~/Content/image/";
                    //上传目录的物理路径
                    string fullUpLoadPath = Server.MapPath(upLoadPath);
                    //核心方法，上传文件并保存
                    postFile.SaveAs(fullUpLoadPath + newFileName);
                    return Json(new { success = true, message = "", remark = "/Content/image/" + newFileName });
                }
                else
                {
                    return Json(new { success = false, message = "未检测到文件数据" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "上传失败:" + ex.Message });
            }
        }

    }
}